﻿using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Jinndev.UI {

    /// <summary>
    /// 日志条目
    /// </summary>
    public class LogItem : MonoBehaviour {

        public UnityAction onDelete;

        public Image imageBg;
        public Image imageIcon;
        public Text textMessage;
        public HorizontalLayoutGroup layoutGroup;
        public float minHeight = 80;

        private RectTransform rectTransform;
        private RectTransform textTransform;

        private void Awake() {
            rectTransform = transform as RectTransform;
            textTransform = textMessage.transform as RectTransform;
        }

        private IEnumerator Start() {
            yield return new WaitForEndOfFrame();
        }

        public void Refresh(string message, Color bgColor, Color textColor, Color iconColor, bool enableDelete, UnityAction onClick) {
            Refresh(message);

            imageBg.color = bgColor;
            textMessage.color = textColor;
            imageIcon.color = iconColor;

            CommonUtil.SetActive(gameObject, "ButtonDelete", enableDelete);
            UIUtil.AddButtonClick(gameObject, null, onClick);
        }

        public void Refresh(string message) {
            textMessage.text = message;
            Vector2 size = rectTransform.sizeDelta;
            LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform);
            //size.y += textMessage.preferredHeight - textTransform.sizeDelta.y;
            size.y = textMessage.preferredHeight + layoutGroup.padding.vertical;
            size.y = Mathf.Max(minHeight, size.y);
            rectTransform.sizeDelta = size;
        }

        public void OnClickDelete() {
            if (onDelete != null) {
                onDelete.Invoke();
            }
        }

        public float Height {
            get { return rectTransform.sizeDelta.y; }
        }

        //public void OnClick() {

        //}

    }

}
